﻿using DevExpress.Data.Filtering;
using DevExpress.Xpo;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class PlanIDBLL
    {
        public static string GetPlanID(Session s,string typeCode,string saleDate="") 
        {
            string date = string.Empty;
            if (string.IsNullOrEmpty(saleDate))
                date = DateTime.Now.ToString("yyMMdd");
            else
                date = Convert.ToDateTime(saleDate).ToString("yyMMdd");
            typeCode = MemberInfo.StoreCode + typeCode;
            S_PlanID planId = s.FindObject<S_PlanID>(CriteriaOperator.Parse("IDType=? And IDDT=?", typeCode, date));
            if (planId != null)
            {
                planId.IDValue = planId.IDValue + 1;
                planId.Save();
            }
            else 
            {
                planId = new S_PlanID(s);
                planId.IDType = typeCode;
                planId.IDDT = date;
                planId.IDValue = 1;
                planId.Save();
            }
            return typeCode + date + planId.IDValue.ToString().PadLeft(4, '0');
        }
    }
}
